﻿using ECG.Common;
using ECG.DAL;
using ECG.Utility;
using ECG.WebUI.CustomHelpers;
using ECG.WebUI.Infrastructure;
using ECG.WebUI.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ECG.WebUI.Content
{
    public class PromotionPageController : Controller
    {
        //
        // GET: /PromotionPage/

        public ActionResult home()
        {
            ViewBag.BannerByCateList = BannerDb.GetAllBanner() ?? new List<ViewBannerClientDto>();
            var model = CacheLayer.Get<List<ProductCategoryByPromotionPageDto>>(ConstVariableList.MemCacheMenuPromotion);
            if (model == null)
            {
                model = ProductCategoryDb.GetAllByPromotionPage() ?? new List<ProductCategoryByPromotionPageDto>();
                CacheLayer.Add(model, ConstVariableList.MemCacheMenuPromotion);
            }
            ViewBag.Menu = model;
            return View();
        }

        public ActionResult header()
        {
            var modelFooter = FunctionHelpers.GetWebInfo();

            return PartialView(modelFooter);
        }
        // chkPaging=true: co hien thi phan trang
        public PartialViewResult BindProduct(int page = 1, string t = "",string category="",bool chkPaging=true)
        {
            var itemsPerPage =32;
            var totalPage = 0;
            var totalRecord = 0;
            var ListProduct =  new List<ProductListByPromotionPageDto>();

            switch (t)
            {
                case "mn": // moi nhat - default
                    ListProduct = ProductDb.GetAllProductByPromotionPage(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "bm": // buy most
                    ListProduct = ProductDb.GetAllProductByPromotionPage_buymost(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "pb": // price best
                    ListProduct = ProductDb.GetAllProductByPromotionPage_priceBest(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "cb": // mua theo goi
                    ListProduct = ProductDb.GetAllProductByPromotionPage_byCate(page, itemsPerPage, ref totalPage, ref totalRecord, category) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "sb": // mua theo set
                    ListProduct = ProductDb.GetAllProductByPromotionPage_byCate(page, itemsPerPage, ref totalPage, ref totalRecord, category) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "cat": // mua theo set
                    ListProduct = ProductDb.GetAllProductByPromotionPage_byCate(page, itemsPerPage, ref totalPage, ref totalRecord, category) ?? new List<ProductListByPromotionPageDto>();
                    break;
                default:
                    ListProduct = ProductDb.GetAllProductByPromotionPage(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
            }
            if (chkPaging)
            {
                ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            }
            ViewBag.TotalRecord = totalRecord; // tong so san pham dang khuyen mai
            ViewBag.type = t; // san pham duco lay theo loai nao ?
            ViewBag.chkPagging = chkPaging; // co cho phep hien thi phan trang co dien hay ko
            ViewBag.Page = page; // trang thien tai
            ViewBag.ItemRecord = ListProduct.Count(); // so ban ghi hien thoi lay duoc
            return PartialView(ListProduct);
        }


        public PartialViewResult BindProductPagging(int page = 1, string t = "", string category = "")
        {
            var itemsPerPage = 32;
            var totalPage = 0;
            var totalRecord = 0;
            var ListProduct = new List<ProductListByPromotionPageDto>();

            switch (t)
            {
                case "mn": // moi nhat - default
                    ListProduct = ProductDb.GetAllProductByPromotionPage(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "bm": // buy most
                    ListProduct = ProductDb.GetAllProductByPromotionPage_buymost(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "pb": // price best
                    ListProduct = ProductDb.GetAllProductByPromotionPage_priceBest(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "cb": // mua theo goi
                    ListProduct = ProductDb.GetAllProductByPromotionPage_byCate(page, itemsPerPage, ref totalPage, ref totalRecord, category) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "sb": // mua theo set
                    ListProduct = ProductDb.GetAllProductByPromotionPage_byCate(page, itemsPerPage, ref totalPage, ref totalRecord, category) ?? new List<ProductListByPromotionPageDto>();
                    break;
                case "cat": // mua theo category
                    ListProduct = ProductDb.GetAllProductByPromotionPage_byCate(page, itemsPerPage, ref totalPage, ref totalRecord, category) ?? new List<ProductListByPromotionPageDto>();
                    break;
                default:
                    ListProduct = ProductDb.GetAllProductByPromotionPage(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ProductListByPromotionPageDto>();
                    break;
            }
            ViewBag.Page = page; // trang thien tai
            ViewBag.ItemRecord = ListProduct.Count(); // so ban ghi hien thoi lay duoc
            return PartialView(ListProduct);
        }

        public JsonResult AddEmail(string email = "")
        {
            var chk = 0; // khong them duoc email
            //var model = ProductDb.GetProductNameListByKeyword(query) ?? new List<ProductNameListDto>();
            //var strArr = model.Select(item => item.Name).ToList();
            //var data = new
            //{
            //    query,
            //    suggestions = strArr
            //};

            // Check email exists
            var hasEmail = CustomerDb.GetByEmail(email);
            if (hasEmail == null)
            {
                var passwordSalt = Guid.NewGuid().ToString();
                var password = "123456".CreatePasswordHash(passwordSalt);
                var customerInfo = new CustomerDto
                {
                    Id = FunctionHelpers.GenerateId(),
                    FullName = email,
                    Password = password,
                    PasswordSalt = passwordSalt,
                    Email = email,
                    Birthday = DateTime.Now,
                    Gender = "Unknow",
                    Address = "Unknow",
                    Telephone = "Unknow",
                    Mobilephone = "Unknow",
                    ProvinceId = 0,
                    ProvinceName = "Unknow",
                    DistrictId = 0,
                    DistrictName = "Unknow",
                };
                CustomerDb.Add(customerInfo);

                chk = 1;
            }

            return Json(chk, JsonRequestBehavior.AllowGet);
        }
    }
}
